# @Auther: cty
# @Date: 2019/10/31
# @Description: 数据库查询， fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据
# @version: 1.0

import pymysql

# 打开数据库连接
db = pymysql.connect("localhost", "cty", "123456", "TESTDB")

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# SQL 查询语句
sql = "SELECT * FROM EMPLOYEE \
       WHERE INCOME > %s" % (1000)
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 获取所有记录列表
   results = cursor.fetchall()
   for row in results:
      fname = row[0]
      lname = row[1]
      age = row[2]
      sex = row[3]
      income = row[4]
       # 打印结果
      print ("fname=%s,lname=%s,age=%s,sex=%s,income=%s" % \
             (fname, lname, age, sex, income ))
except:
   print ("Error: unable to fetch data")

# 关闭数据库连接
db.close()



'''
# 结果：
fname=Mac,lname=Mohan,age=20,sex=M,income=2000.0
'''